<?php

declare(strict_types=1);

namespace App\Chat\Support;


use App\Chat\Constant\TalkModeConstant;
use App\Chat\Service\GroupMemberService;
use App\Chat\Service\UserFriendService;
use App\CustHub\Model\ChCustomerService;

class UserRelation
{
    /**
     * 判断是否是好友或者群成员关系
     *
     * @param int $user_id     用户ID
     * @param int $receiver_id 接收者ID
     * @param int $talk_type   对话类型
     * @return bool
     */
    public static function isFriendOrGroupMember(int $user_id, int $robotId, int $receiver_id, int $talk_type): bool
    {
        //判断当前机器人是否归属当前客服
        $exists = ChCustomerService::query()
            ->where('id',$user_id)
            ->whereHas('robots',function ($query) use ($robotId){
                $query->where('robot_id',$robotId);
            })->exists();

        if(!$exists){
            return false;
        }

        if ($talk_type == TalkModeConstant::PRIVATE_CHAT) {
            return di()->get(UserFriendService::class)->isFriend($robotId, $receiver_id);
        } else if ($talk_type == TalkModeConstant::GROUP_CHAT) {
            return di()->get(GroupMemberService::class)->isMember($receiver_id, $robotId);
        }

        return false;
    }
}